package com.alex.DAO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.JDBCException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.alex.entity.Board;


public class BoardDAO extends BaseDAO implements IBoardDAO {
	private static final Log log = LogFactory.getLog(BoardDAO.class);
	private HashMap map = new HashMap();
	private int parentId = 0;
	private List sonList = null;
//	HibernateUtil util = new HibernateUtil();

	/*/
	 * 
	 */
	public Map findBoard() throws Exception {
		Session session = getSession();
		List list = null;
		Transaction tr = null;
		sonList = new ArrayList();
		tr = session.beginTransaction();
		String sql = "from Board b order by parentId,boardid";
		try {
			Query query = session.createQuery(sql);
			list = query.list();
			for (int i = 0; i < list.size(); i++) {
				Board board = (Board) list.get(i);
				if (parentId != board.getParentid()) {
					map.put(new Integer(parentId), sonList);
					sonList = new ArrayList();
					parentId = board.getParentid();
				}
				Board board2 = new Board();
				board2.setBoardid(board.getBoardid());
				board2.setBoardname(board.getBoardname());
				board2.setParentid(board.getParentid());
				sonList.add(board2);
			}
			map.put(new Integer(parentId), sonList);
			session.close();
		} catch (JDBCException ex) {
			if (tr != null)
				tr.rollback();
			throw ex.getSQLException();
		}
		return map;
	}

	/*/
	 * 
	 */
	public Board findBoardByid(int boardid) throws Exception {
		Session session = getSession();
		Transaction tr = null;
		Board board = null;
		String hql = "from Board b where b.boardid='" + boardid + "' ";
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			List list = query.list();
			board = (Board) list.get(0);
			tr.commit();
			session.close();
		} catch (JDBCException ex) {
			if (tr != null)
				tr.rollback();
			throw ex.getSQLException();
		}
		return board;
	}

	/*/
	 * 
	 */
	public void BoardSave(Board board) throws Exception {
		Session session = getSession();
		Transaction tr = null;
		try {
			tr = session.beginTransaction();
			session.save(board);
			tr.commit();
			session.close();
		} catch (JDBCException ex) {
			if (tr != null)
				tr.rollback();
			throw ex.getSQLException();
		}
	}

	/*/
	 * 
	 */
	public void BoardUpdate(Board board) throws Exception {
		Session session = getSession();
		Transaction tr = null;
		try {
			tr = session.beginTransaction();
			session.saveOrUpdate(board);
			tr.commit();
			session.close();
		} catch (JDBCException ex) {
			if (tr != null)
				tr.rollback();
			throw ex.getSQLException();
		}
	}

	/**
	 * 删除板块
	 * 
	 * @param boardid
	 * @throws Exception
	 */
	public void BoardDelete(int boardid) throws Exception {
		Session session = getSession();
		Transaction tr = null;
		try {
			tr = session.beginTransaction();
			session.delete(this.findBoardByid(boardid));
			tr.commit();
			session.close();
		} catch (JDBCException ex) {
			if (tr != null)
				tr.rollback();
			throw ex.getSQLException();
		}
	}

	/**
	 * 找府板块
	 * 
	 * @return
	 */
	public List findparentboard() {
		Session session = getSession();
		Transaction tr = null;
		List result = null;
		try {
			tr = session.beginTransaction();
			String hql = "from Board b where b.parentid=0";
			Query query = session.createQuery(hql);
			result = query.list();
			tr.commit();
			session.close();
			return result;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}